package com.spic.business.plan.emergencycode.service.impl;

import com.spic.business.common.core.utils.DateUtil;
import com.spic.business.plan.emergencycode.domain.EmergencyCode;
import com.spic.business.plan.emergencycode.domain.ExaMine;
import com.spic.business.plan.emergencycode.domain.MaterialsCode;
import com.spic.business.plan.emergencycode.mapper.EmergencyCodeMapper;
import com.spic.business.plan.emergencycode.service.IEmergencyCodeService;
import com.spic.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 应急物资编码Service业务层处理
 *
 * @author qishaoping
 * @date 2021-10-28
 */
@Service
public class EmergencyCodeServiceImpl implements IEmergencyCodeService
{
    @Autowired
    private EmergencyCodeMapper emergencyCodeMapper;
    @Transactional
    public String getCodeName (){
        String material = MaterialsCode.getUpperHeadChar(ExaMine.emergency_material);
        String year = DateUtil.format(new Date(),"yyyy-MM-dd");
        StringBuffer stringBuffer = new StringBuffer();
        EmergencyCode emergencyCode = new EmergencyCode();
        //时间
        emergencyCode.setMonth(year);
        //编码名称
        emergencyCode.setMaterials(material);
        List<EmergencyCode> emergencyCodes = emergencyCodeMapper.selectEmergencyCodeList(emergencyCode);
        if(emergencyCodes!=null && emergencyCodes.size()>0){
            EmergencyCode emergencyCodeList = emergencyCodes.get(0);
            //获取序列号
            String maxNumber = emergencyCodeList.getMaxNumber();
            //获取自增序列号
            String nextNumber = getNext(maxNumber);
            //获取时间
            String  years = emergencyCodeList.getMonth();

            //拼接
            StringBuffer serialNumbers = stringBuffer.append(material).append("-").append(years).append("-").append(nextNumber);
            //转化字符串
            String serialNumber = serialNumbers.toString();
            //对象设置值
            EmergencyCode emergencyCode1 =  getTicketCodings(material,nextNumber,years,serialNumber);
            emergencyCodeMapper.insertEmergencyCode(emergencyCode1);
            return serialNumber;
        }else{
            String nextNumber = getNext(MaterialsCode.BIGINCODE);
            stringBuffer = new StringBuffer();
            StringBuffer serialNumbers = stringBuffer.append(material).append("-").append(year).append("-").append(nextNumber);
            String serialNumber = serialNumbers.toString();
            EmergencyCode emergencyCode2 =  getTicketCodings(material,nextNumber,year,serialNumber);
            emergencyCodeMapper.insertEmergencyCode(emergencyCode2);
            return serialNumber;
        }

    }
    @Transactional
    public String getCodeNames (){
        String material = MaterialsCode.getUpperHeadChar(ExaMine.emergency_inspection);
        String year = DateUtil.format(new Date(),"yyyy-MM-dd");
        StringBuffer stringBuffer = new StringBuffer();
        EmergencyCode emergencyCode = new EmergencyCode();
        //时间
        emergencyCode.setMonth(year);
        //编码名称
        emergencyCode.setMaterials(material);
        List<EmergencyCode> emergencyCodes = emergencyCodeMapper.selectEmergencyCodeList(emergencyCode);
        if(emergencyCodes!=null && emergencyCodes.size()>0){
            EmergencyCode emergencyCodeList = emergencyCodes.get(0);
            //获取序列号
            String maxNumber = emergencyCodeList.getMaxNumber();
            //获取自增序列号
            String nextNumber = getNext(maxNumber);
            //获取时间
            String  years = emergencyCodeList.getMonth();

            //拼接
            StringBuffer serialNumbers = stringBuffer.append(material).append("-").append(years).append("-").append(nextNumber);
            //转化字符串
            String serialNumber = serialNumbers.toString();
            //对象设置值
            EmergencyCode emergencyCode1 =  getTicketCodings(material,nextNumber,years,serialNumber);
            emergencyCodeMapper.insertEmergencyCode(emergencyCode1);
            return serialNumber;
        }else{
            String nextNumber = getNext(MaterialsCode.BIGINCODE);
            stringBuffer = new StringBuffer();
            StringBuffer serialNumbers = stringBuffer.append(material).append("-").append(year).append("-").append(nextNumber);
            String serialNumber = serialNumbers.toString();
            EmergencyCode emergencyCode2 =  getTicketCodings(material,nextNumber,year,serialNumber);
            emergencyCodeMapper.insertEmergencyCode(emergencyCode2);
            return serialNumber;
        }

    }
    private EmergencyCode getTicketCodings(String material,String nextNumber, String years, String serialNumber) {
        EmergencyCode emergencyCode= new EmergencyCode();
        emergencyCode.setMonth(years);
        emergencyCode.setMaxNumber(nextNumber);
        emergencyCode.setSerialNumber(serialNumber);
        emergencyCode.setMaterials(material);
        return  emergencyCode;

    }
    private String getNext(String maxNumber) {
        int startValue = Integer.parseInt(maxNumber);
        AtomicInteger val = new AtomicInteger(startValue);
        int nextVal = val.incrementAndGet();
        return String.format("%04d", nextVal);
    }

    /**
     * 查询应急物资编码
     *
     * @param id 应急物资编码主键
     * @return 应急物资编码
     */
    @Override
    public EmergencyCode selectEmergencyCodeById(Long id)
    {
        return emergencyCodeMapper.selectEmergencyCodeById(id);
    }

    /**
     * 查询应急物资编码列表
     *
     * @param emergencyCode 应急物资编码
     * @return 应急物资编码
     */
    @Override
    public List<EmergencyCode> selectEmergencyCodeList(EmergencyCode emergencyCode)
    {
        return emergencyCodeMapper.selectEmergencyCodeList(emergencyCode);
    }

    /**
     * 新增应急物资编码
     *
     * @param emergencyCode 应急物资编码
     * @return 结果
     */
    @Override
    public int insertEmergencyCode(EmergencyCode emergencyCode)
    {
        emergencyCode.setCreateTime(DateUtils.getNowDate());
        return emergencyCodeMapper.insertEmergencyCode(emergencyCode);
    }

    /**
     * 修改应急物资编码
     *
     * @param emergencyCode 应急物资编码
     * @return 结果
     */
    @Override
    public int updateEmergencyCode(EmergencyCode emergencyCode)
    {
        emergencyCode.setUpdateTime(DateUtils.getNowDate());
        return emergencyCodeMapper.updateEmergencyCode(emergencyCode);
    }

    /**
     * 批量删除应急物资编码
     *
     * @param ids 需要删除的应急物资编码主键
     * @return 结果
     */
    @Override
    public int deleteEmergencyCodeByIds(Long[] ids)
    {
        return emergencyCodeMapper.deleteEmergencyCodeByIds(ids);
    }

    /**
     * 删除应急物资编码信息
     *
     * @param id 应急物资编码主键
     * @return 结果
     */
    @Override
    public int deleteEmergencyCodeById(Long id)
    {
        return emergencyCodeMapper.deleteEmergencyCodeById(id);
    }
}
